package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("findAll")
    public Result<List<Brand>> findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", brandList);
    }

    @GetMapping("{id}")
    public Result<Brand> findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", brand);
    }

    @PostMapping("insert")
    public Result insert(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    @PutMapping("update/{id}")
    public Result update(@PathVariable Integer id, @RequestBody Brand brand) {
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {
        brandService.deleteById(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @GetMapping("search")
    public Result search(@RequestParam Map map) {
        System.out.println(map);
        List list = brandService.search(map);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping("search/{pageNum}/{pageSize}")
    public Result search(@PathVariable int pageNum, @PathVariable int pageSize) {
        Page<Brand> searchPage = brandService.searchPage(pageNum, pageSize);
        PageResult<Brand> pageResult = new PageResult<>(searchPage.getTotal(), searchPage.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    //    品牌列表条件+分页查询
    @GetMapping("search1/{pageNum}/{pageSize}")
    public Result search(@PathVariable int pageNum, @PathVariable int pageSize, @RequestParam Map map) {
        Page page = brandService.searchPageByLike(pageNum, pageSize, map);
        PageResult<Brand> pageResult = new PageResult<>(page.getTotal(), page.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }
}
